//
//  DictionaryExtension.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit

extension Dictionary {
    //用于字典的合并，接收的参数是一个键值对时，就可以添加到原有的字典中，并且对原有字典的重复值进行覆盖为新值，不重复则保留
    mutating func wn_merge<S>(_ other: S)
        where S: Sequence, S.Iterator.Element == (key: Key, value: Value) {
            for (k ,v) in other {
                self[k] = v
        }
    }
    
    func toJsonString() -> String? {
        guard let data = try? JSONSerialization.data(withJSONObject: self, options: []) else {
            return nil
        }
        guard let str = String(data: data, encoding: .utf8) else {
            return nil
        }
        return str
     }
}
